package wiz.client;

import wiz.client.activities.WizPlaceHistoryMapper;
import wiz.client.activities.entrypoint.EntryPointView;
import wiz.client.activities.finishedgame.FinishedGameView;
import wiz.client.activities.playgame.GameSummaryView;
import wiz.client.activities.playhand.PlayHandView;
import wiz.client.activities.selectplayers.SelectPlayersView;
import wiz.client.model.WizModelFactory;
import wiz.client.storage.PageSessionStateStorage;
import wiz.client.ui.viewimpl.EntryPointViewImpl;
import wiz.client.ui.viewimpl.FinishedGameViewImpl;
import wiz.client.ui.viewimpl.GameSummaryViewImpl;
import wiz.client.ui.viewimpl.PlayHandViewImpl;
import wiz.client.ui.viewimpl.SelectPlayersViewImpl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.storage.client.Storage;
import com.google.web.bindery.event.shared.EventBus;

public class ClientFactory {
	private EventBus fEventBus = new SimpleEventBus();
	private WizModelFactory fWizModelFactory = new WizModelFactory();
	
	private WizGameStateStorage fStorage;
	
	private PlaceController fPlaceController = new PlaceController(fEventBus);
	private PlaceHistoryMapper fWizPlaceHistoryMapper = GWT.create(WizPlaceHistoryMapper.class);
	private WizServiceAsync fWizService = GWT.create(WizService.class);
	private WizIntegration fRemoteIntegration;
	

	public EventBus getEventBus() { 
		return fEventBus;
	}
	
	public WizGameStateStorage getGameStateStorage() {
		
		if(fStorage == null){
			fStorage = createStorage();
		}
		return fStorage;
	}

	private WizGameStateStorage createStorage() {
		//TODO: Separate state storage from state serialization/parsing
		 Storage storage = Storage.getLocalStorageIfSupported();
		 if(storage == null){
			 storage = Storage.getSessionStorageIfSupported();
		 }
		 if(storage != null){
			 return new LocalStorageWizGameStorage(storage, this); 
		 } else {
			 return new PageSessionStateStorage(this);
		 }
	}

	public PlaceController getPlaceController() {
		return fPlaceController;
	}

	public PlaceHistoryMapper getPlaceHistoryMapper() {
		return fWizPlaceHistoryMapper;
	}

	public EntryPointView buildEntryPointView() {
		return new EntryPointViewImpl();
	}

	public SelectPlayersView buildSelectPlayersView() {
		return new SelectPlayersViewImpl();
	}

	public GameSummaryView buildGameSummaryView() {
		return new GameSummaryViewImpl();
	}

	public PlayHandView buildPlayHandView() {
		return new PlayHandViewImpl();
	}

	public FinishedGameView buildFinishedGameView() {
		return new FinishedGameViewImpl();
	}

	public WizModelFactory getWizModelFactory() {
		return fWizModelFactory;
	}
	
	public GameDetails getWizGameDetails() {
		return new PSSWizGameDetails();
	}
	
	public WizIntegration getRemoteSystemIntegration() {
		if(fRemoteIntegration == null){
			fRemoteIntegration = buildRemoteSystemIntegration();
		}
		return fRemoteIntegration;
	}
	
	private WizIntegration buildRemoteSystemIntegration() {
		if(fWizService != null){
			//TODO: test viability of RPC
			return new ChruszczRemoteWizIntegration(fWizService);
		} else {
			return new DefaultRemoteWizIntegration();
		}
		
	}

}
